package com.gxljc.bear.crawler.util;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

/**
 * 采用java-mail发送邮件 Created by Administrator -> haitao ->haitao on 2016/9/4.
 */
public class MailUtil {
	final static Logger LOG = Logger.getLogger(MailUtil.class);

	public static void main(String[] args) throws Exception {
		LOG.info("-- start --");
		sendMail("aaa", "bbb", "haitao@summba.com", "soft03_test@sina.com");
		LOG.info("-- finish --");
	}

	/**
	 * @param subject
	 * @param content
	 * @param mailTo
	 * @return true if mail sent successfully Example: sendMail(
	 *         "this is subject","this is content","haitao@summba.com")
	 */
	public static boolean sendMail(String subject, String content, String... mailTo) {
		if (subject == null || subject.isEmpty()) {
			subject = "this is subject";
		}

		if (content == null || content.isEmpty()) {
			content = "this is content";
		}
		MailServer server = new MailServer("smtp.sina.com", "soft03_test", "madeinsummba");
		MailMessage msg = new MailMessage(subject, content, "soft03_test@sina.com");
		int mLength = mailTo.length;
		InternetAddress[] addresses = new InternetAddress[mLength];
		try {
			for (int i = 0; i < mailTo.length; i++) {
				addresses[i] = new InternetAddress(mailTo[i]);
			}
		} catch (AddressException e) {
			e.printStackTrace();
		}
		return sendMail(server, msg, addresses);

	}

	/**
	 * default send to sb.
	 * 
	 * @param subject
	 * @param content
	 * @return
	 */
	public static boolean sendMail(String subject, String content) {
		return sendMail(subject, content, "haitao@summba.com", "yujunjie@summba.com");
	}

	/**
	 * send mail to someone！
	 * 
	 * @param subject
	 * @param content
	 * @param address
	 * @return
	 */
	public static boolean sendMailMoreThanOne(String subject, String content, String... address) {
		boolean sendFlag = false;
		int count = 0;
		for (String ad : address) {
			if (sendMail(subject, content, ad)) {
				LOG.info(String.format("send mail to %s", ad));
				count += 1;
			}
		}
		if (count == address.length)
			sendFlag = true;
		return sendFlag;
	}

	public static boolean sendMail(MailServer server, MailMessage msg, Address... addresses) {
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", "smtp");
		properties.setProperty("mail.smtp.auth", "true"); // add 'auth' from
															// overstack
															// suggestion
		Session session = Session.getInstance(properties);
		// 创建Message对象
		Message message = new MimeMessage(session);
		try {
			message.setSubject(msg.subject);
			message.setFrom(new InternetAddress(msg.from));
			message.setContent(msg.content + "\n----" + new java.util.Date(), "text/html;charset=utf-8");

			// 创建接口，并连接（登录）
			Transport transport = session.getTransport();
			transport.connect(server.host, server.user, server.pass);
			// 发送邮件
			transport.sendMessage(message, addresses);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return true;
	}

	static class MailServer {
		String host;
		String user;
		String pass;

		public MailServer(String host, String user, String pass) {
			this.host = host;
			this.user = user;
			this.pass = pass;
		}
	}

	static class MailMessage {
		String subject;
		String from;
		String content;

		public MailMessage(String subject, String content, String from) {
			this.subject = subject;
			this.content = content;
			this.from = from;
		}
	}

}
